<?php
/**
 * Created by PhpStorm.
 * User: daojia
 * Date: 2020/5/8
 * Time: 12:21
 */
namespace app\api\model\pay;
use app\common\model\pay\Order as OrderModel;
use app\common\service\Order as OrderService;
use app\common\enum\pay\order\PayStatus as PayStatusEnum;
class Order extends OrderModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'wxapp_id',
    ];
    /**
     * 创建充值订单
     * @param \app\api\model\User $user 当前用户信息
     * @param int $planId 套餐id
     * @param double $customMoney 自定义充值金额
     * @return bool|false|int
     * @throws BaseException
     * @throws \think\exception\DbException
     */
    public function createOrder($shopid,$user, $customMoney = 0.00)
    {
        // 验证用户输入
        if (!$this->validateForm($customMoney)) {
            $this->error = $this->error ?: '数据验证错误';
            return false;
        }
        // 获取订单数据
        $data = $this->getOrderData($shopid,$user, $customMoney);
        // 记录订单信息
        return $this->saveOrder($data);
    }
    /**
     * 保存订单记录
     * @param $data
     * @return bool|false|int
     */
    private function saveOrder($data)
    {
        // 写入订单记录
        $this->save($data['order']);
        return true;
    }
    /**
     * 生成充值订单
     * @param $user
     * @param $rechargeType
     * @param $planId
     * @param $customMoney
     * @return array
     * @throws BaseException
     * @throws \think\exception\DbException
     */
    private function getOrderData($shopid,$user, $customMoney)
    {
        // 订单信息
        $data = [
            'order' => [
                'user_id' => $user['user_id'],
                'user_pay_id' => $user['userPayId'],
                'shopid' => $shopid,
                'order_no' => 'PAY' . OrderService::createOrderNo(),
                'gift_money' => 0,
                'wxapp_id' => self::$wxapp_id,
            ]
        ];
        // 支付金额
        $this->createDataByCustom($data, $customMoney);
        return $data;
    }
    /**
     * 创建支付订单数据
     * @param $order
     * @param $customMoney
     * @return bool
     */
    private function createDataByCustom(&$order, $customMoney)
    {
        // 用户支付金额
        $order['order']['pay_price'] = $customMoney;
        $order['order']['gift_money'] = integralJiSuan($customMoney);
        return true;
    }
    /**
     * 表单验证
     * @param $rechargeType
     * @param $planId
     * @param $customMoney
     * @return bool
     */
    private function validateForm($customMoney)
    {
        if ($customMoney <= 0) {
            $this->error = '请输入支付金额';
            return false;
        }
        return true;
    }
    /**
     * 获取订单详情(待付款状态)
     * @param $orderNo
     * @return self|null
     * @throws \think\exception\DbException
     */
    public static function getPayDetail($orderNo)
    {
        return self::detail(['order_no' => $orderNo, 'pay_status' => PayStatusEnum::PENDING]);
    }
    /**
     * 获取订单详情
     * @param $orderNo
     * @return self|null
     * @throws \think\exception\DbException
     */
    public static function getPayDetailId($id)
    {
        return self::detail(['order_id' => $id]);
    }
}